clear all
set more off


cd "C:\Dropbox\Average_target_survey\replication_folder\rawdata"

import excel using "frbny-sce-public-microdata-latest", clear cellrange("A2") firstrow

keep if (survey_date>= 22128 & survey_date<=22172) 

char survey_date[omit] 22153


**==============================================================================
***				implied mean 1-year ahead
**==============================================================================

xi: rreg Q9_mean i.survey_date, gen(wgt0)
xi: reg Q8v2part2 i.survey_date [aw=wgt0]


gen b0_Q9_mean=.
gen se0_Q9_mean=.

replace b0_Q9_mean=0 if survey_date==22153
replace se0_Q9_mean=0 if survey_date==22153

forvalues i=22128(1)22172 {
    capture {
		replace b0_Q9_mean=_b[_Isurvey_da_`i'] if survey_date==`i'
		replace se0_Q9_mean=_se[_Isurvey_da_`i'] if survey_date==`i'
	}
}

**==============================================================================
***				implied mean 3-year ahead
**==============================================================================

xi: rreg Q9c_mean i.survey_date, gen(wgt1)
xi: reg Q9c_mean i.survey_date [aw=wgt1], robust

gen b0_Q9c_mean=.
gen se0_Q9c_mean=.

replace b0_Q9c_mean=0 if survey_date==22153
replace se0_Q9c_mean=0 if survey_date==22153

forvalues i=22128(1)22172 {
    capture {
		replace b0_Q9c_mean=_b[_Isurvey_da_`i'] if survey_date==`i'
		replace se0_Q9c_mean=_se[_Isurvey_da_`i'] if survey_date==`i'
	}
}


**==============================================================================
***				point prediction 1-year ahead
**==============================================================================

replace Q8v2part2=. if Q8v2part2>50 | Q8v2part2<-10

xi: rreg Q8v2part2 i.survey_date, gen(wgt2)
xi: reg Q8v2part2 i.survey_date [aw=wgt2], robust

gen b0_Q8v2part2=.
gen se0_Q8v2part2=.

replace b0_Q8v2part2=0 if survey_date==22153
replace se0_Q8v2part2=0 if survey_date==22153

forvalues i=22128(1)22172 {
    capture {
		replace b0_Q8v2part2=_b[_Isurvey_da_`i'] if survey_date==`i'
		replace se0_Q8v2part2=_se[_Isurvey_da_`i'] if survey_date==`i'
	}
}


collapse (mean) b0* se0* ///
	     (mean) Q9c_mean Q9_mean Q8v2part2 ///
		 (count) Ncount=Q8v2part2, ///
		 by(survey_date)

gen b0_Q8v2part2_high=b0_Q8v2part2+1.96*se0_Q8v2part2
gen b0_Q8v2part2_low=b0_Q8v2part2-1.96*se0_Q8v2part2

gen b0_Q9c_mean_high=b0_Q9c_mean+1.96*se0_Q9c_mean
gen b0_Q9c_mean_low=b0_Q9c_mean-1.96*se0_Q9c_mean

gen b0_Q9_mean_high=b0_Q9_mean+1.96*se0_Q9_mean
gen b0_Q9_mean_low=b0_Q9_mean-1.96*se0_Q9_mean
		 
twoway (line b0_Q8v2part2 survey_date, lpattern(solid) lcolor(black) lwidth(thick)) ///
		(line b0_Q8v2part2_high survey_date, lpattern(dash) lcolor(black)) ///
		(line b0_Q8v2part2_low survey_date, lpattern(dash) lcolor(black)) ///
		, ///
		graphregion(color(white)) bgcolor(white) ///
		legend(off) ///
		xtitle("") ytitle("percent") ///
		xline(22153) ///
		title("Panel A: 1-year ahead E{&pi}, point forecast", size(med)) ///
		name(fig1, replace)
		
	
twoway (line b0_Q9_mean survey_date, lpattern(solid) lcolor(black) lwidth(thick)) ///
		(line b0_Q9_mean_high survey_date, lpattern(dash) lcolor(black)) ///
		(line b0_Q9_mean_low survey_date, lpattern(dash) lcolor(black)) ///
		, ///
		graphregion(color(white)) bgcolor(white) ///
		legend(off) ///
		xtitle("") ytitle("percent") ///
		xline(22153) ///
		title("Panel B: 1-year ahead E{&pi}, implied mean", size(med)) ///
		name(fig2, replace)
		
		
twoway (line b0_Q9c_mean survey_date, lpattern(solid) lcolor(black) lwidth(thick)) ///
		(line b0_Q9c_mean_high survey_date, lpattern(dash) lcolor(black)) ///
		(line b0_Q9c_mean_low survey_date, lpattern(dash) lcolor(black)) ///
		, ///
		graphregion(color(white)) bgcolor(white) ///
		legend(off) ///
		xtitle("") ytitle("percent") ///
		xline(22153) ///
		title("Panel C: 3-year ahead E{&pi}, implied mean", size(med)) ///
		name(fig3, replace)
	
	
twoway (line Ncount survey_date, lpattern(solid) lcolor(blue) lwidth(thick)) ///
		, ///
		graphregion(color(white)) bgcolor(white) ///
		legend(off) ///
		xtitle("") ytitle("count") ///
		xline(22153) ///
		title("Panel D: Daily survey responses", size(med)) ///
		name(fig4, replace)
		
		
graph combine fig1 fig2 fig3 fig4, ///
		imargin(tiny) graphregion(color(white))  ///
		name(fig5, replace) ///
		xsize(36) ysize(26)
				
		
		 
		 